<template>
  <div class="overview-container">
    <div class="page-header">
      <h2>数据概览</h2>
      <div class="header-actions">
        <el-button class="dashboard-btn" type="danger" @click="goTo3D">
          <el-icon><DataBoard /></el-icon>
          数据大屏
        </el-button>
        <el-tooltip content="刷新数据" placement="top">
          <el-button circle @click="refreshData">
            <el-icon><Refresh /></el-icon>
          </el-button>
        </el-tooltip>
      </div>
    </div>
    
    <!-- 数据卡片区域 -->
    <div class="stat-cards">
      <!-- 平台销售额 -->
      <el-card class="stat-card sales-card" shadow="hover">
        <div class="card-icon">
          <el-icon><Money /></el-icon>
        </div>
        <div class="card-content">
          <div class="card-title">平台总销售额</div>
          <div class="card-value">{{ totalSales }} 万元</div>
        </div>
      </el-card>
      
      <!-- 平台总销售量 -->
      <el-card class="stat-card orders-card" shadow="hover">
        <div class="card-icon">
          <el-icon><ShoppingCart /></el-icon>
        </div>
        <div class="card-content">
          <div class="card-title">平台总销售量</div>
          <div class="card-value">{{ totalOrders }} 件</div>
        </div>
      </el-card>
      
      <!-- 商户总数 -->
      <el-card class="stat-card merchants-card" shadow="hover">
        <div class="card-icon">
          <el-icon><Shop /></el-icon>
        </div>
        <div class="card-content">
          <div class="card-title">商户总数</div>
          <div class="card-value">{{ merchantTotal }} 家</div>
        </div>
      </el-card>
      
      <!-- 用户总数 -->
      <el-card class="stat-card users-card" shadow="hover">
        <div class="card-icon">
          <el-icon><User /></el-icon>
        </div>
        <div class="card-content">
          <div class="card-title">用户总数</div>
          <div class="card-value">{{ totalUsers }} 人</div>
        </div>
      </el-card>
      
      <!-- 商品总数 -->
      <el-card class="stat-card products-card" shadow="hover">
        <div class="card-icon">
          <el-icon><Goods /></el-icon>
        </div>
        <div class="card-content">
          <div class="card-title">商品总数</div>
          <div class="card-value">{{ productTotal }} 件</div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { 
  Money, 
  ShoppingCart, 
  Shop, 
  User, 
  Goods,
  Refresh,
  DataBoard
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { 
  getProductTotal, 
  getTotalSalesVolume, 
  getTotalSales, 
  getTotalMerchants, 
  getHTotalUsers
} from '../../api/auth'


const router = useRouter()
const productTotal = ref(0)
const totalOrders = ref(0)
const totalUsers = ref(0)
const totalSales = ref('0.00')
const merchantTotal = ref(0)

// 跳转到数据大屏页面
const goTo3D = () => {
  router.push('/zenosales3d')
}

// 获取数据函数
const getTotal = async () => {
  const res = await getProductTotal()
  productTotal.value = res.data.data
}

const getSalesVolume = async () => {
  const res = await getTotalSalesVolume()
  totalOrders.value = res.data.data
}

const getUsers = async () => {
  try {
    const res = await getHTotalUsers()
    if (res.data?.code === 200) {
      totalUsers.value = res.data.data
    }
  } catch (error) {
    console.error('获取用户总数失败:', error)
    totalUsers.value = 0
  }
}

const getSales = async () => {
  try {
    const res = await getTotalSales()
    if (res.data?.code === 200) {
      // 将销售额转换为万元单位，并添加千位分隔符
      const salesValue = parseFloat(res.data.data) / 10000;
      totalSales.value = salesValue.toLocaleString('zh-CN', { 
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      });
    }
  } catch (error) {
    console.error('获取销售额失败:', error);
  }
}

const getMerchants = async () => {
  const res = await getTotalMerchants()
  merchantTotal.value = res.data.data
}

// 刷新所有数据
const refreshData = async () => {
  await Promise.all([
    getTotal(),
    getSalesVolume(),
    getUsers(),
    getSales(),
    getMerchants()
  ])
}

onMounted(() => {
  refreshData()
})
</script>

<style scoped>
.overview-container {
  padding: 30px;
  background-color: #f5f5f5;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  font-size: 24px;
  font-weight: bold;
  color: #333;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.dashboard-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  background-color: #e6333e;
  border-color: #e6333e;
}

.mt-20 {
  margin-top: 20px;
}

/* 数据卡片样式 */
.stat-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 30px;
  margin-bottom: 40px;
  padding: 20px;
  flex: 1;
}

.stat-card {
  display: flex;
  align-items: center;
  padding: 40px;
  border-radius: 16px;
  background-color: #fff;
  box-shadow: 0 8px 24px 0 rgba(0, 0, 0, 0.15);
  transition: all 0.3s ease;
  height: 240px;
}

.stat-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 15px 30px rgba(0,0,0,0.15);
}

.card-icon {
  width: 70px;
  height: 70px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 25px;
  color: #fff;
  font-size: 32px;
}

.card-content {
  flex: 1;
}

.card-title {
  font-size: 18px;
  color: #606266;
  margin-bottom: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-value {
  font-size: 28px;
  font-weight: bold;
  color: #303133;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 卡片颜色样式 */
.sales-card .card-icon {
  background-color: #409EFF;
}

.orders-card .card-icon {
  background-color: #67C23A;
}

.merchants-card .card-icon {
  background-color: #E6A23C;
}

.users-card .card-icon {
  background-color: #F56C6C;
}

.products-card .card-icon {
  background-color: #67C23A;
}

/* 图表容器样式 */
.charts-container {
  width: 100%;
}

.charts-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}

.chart-card {
  border-radius: 4px;
  border: none;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.chart-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}

.chart-header {
  padding: 16px 20px;
  border-bottom: 1px solid #EBEEF5;
}

.chart-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
}

.chart-content {
  padding: 20px;
  min-height: 300px;
}

.echarts-container {
  width: 100%;
  height: 300px;
}

.distribution-card {
  border-radius: 4px;
  border: none;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.distribution-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}

.distribution-header {
  padding: 16px 20px;
  border-bottom: 1px solid #EBEEF5;
}

.distribution-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
}

.distribution-content {
  padding: 20px;
  min-height: 300px;
}

.pie-chart-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 40px;
}

.pie-chart {
  width: 220px;
  height: 220px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.pie-segment {
  width: 220px;
  height: 220px;
  border-radius: 50%;
  background: conic-gradient(
    #409EFF 0% 25%,
    #67C23A 25% 45%,
    #E6A23C 45% 63%,
    #F56C6C 63% 85%,
    #909399 85% 100%
  );
}

.pie-legend {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.legend-item {
  display: flex;
  align-items: center;
}

.legend-color {
  width: 16px;
  height: 16px;
  border-radius: 3px;
  margin-right: 10px;
}

.legend-text {
  font-size: 14px;
  color: #606266;
}

/* 响应式布局已删除 */

.category-chart-container {
  display: flex;
  height: 300px;
  width: 100%;
}

.pie-chart .echarts-container {
  width: 100%;
  height: 100%;
}

.distribution-content {
  padding: 20px;
  min-height: 400px;
}

/* 平台概况部分 */
.section-container {
  background-color: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-title {
  display: flex;
  align-items: center;
  font-size: 18px;
  font-weight: 500;
  color: #303133;
}

.section-title-line {
  width: 4px;
  height: 18px;
  background-color: #e6333e;
  margin-right: 10px;
  border-radius: 2px;
} 
</style>
 